package com.example.test_app.view;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.animation.drawable.AnimatedDrawable2;
import com.facebook.fresco.animation.drawable.AnimationListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class SimpleWebpView extends SimpleDraweeView {
    private PipelineDraweeControllerBuilder builder;
    private AnimatedDrawable2 mAnimatable = null;
    private int  nobb=0;


    public SimpleWebpView(Context context) {
        super(context);
    }

    public SimpleWebpView(Context context, AttributeSet attrs) {
        super(context, attrs);


    }


    public int getNobb() {
        return nobb;
    }

    public void setNobb(int nobb) {
        Log.d(getClass().getSimpleName(), "setNobb: "+nobb);
        this.nobb = nobb;
    }

    public void loadAsset(String assetName) {
        Uri uri = Uri.parse("asset:///" + assetName);
        load(uri);
    }

    public void loadRes(int resId) {
        Uri uri = Uri.parse("res:///" + resId);
        load(uri);
    }

    public void loadNet(String url) {
        Uri uri = Uri.parse(url);
        load(uri);
    }

    private void load(Uri uri) {
        ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(uri);
        builder = Fresco.newDraweeControllerBuilder();
        builder.setImageRequest(imageRequestBuilder.build());
        this.setController(builder.build());
    }

    public void setAutoPlay(boolean isAuto, AnimationListener listener) {
        builder.setAutoPlayAnimations(isAuto);
        builder.setControllerListener(new ControllerListener<ImageInfo>() {
            @Override
            public void onSubmit(String id, Object callerContext) {

            }

            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                Log.d("setAutoPlayControRepeat","onFinalImageSet");
                AnimatedDrawable2 drawable2 = (AnimatedDrawable2) animatable;
                mAnimatable=drawable2;
                if(listener!=null){
                    drawable2.setAnimationListener(listener);
                }

            }

            @Override
            public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

            }

            @Override
            public void onIntermediateImageFailed(String id, Throwable throwable) {

            }

            @Override
            public void onFailure(String id, Throwable throwable) {

            }

            @Override
            public void onRelease(String id) {

            }
        });

        this.setController(builder.build());
    }

    public void setAutoPlay(boolean isAuto) {
        builder.setAutoPlayAnimations(isAuto);
        builder.setControllerListener(new ControllerListener<ImageInfo>() {
            @Override
            public void onSubmit(String id, Object callerContext) {

            }

            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                AnimatedDrawable2 drawable2 = (AnimatedDrawable2) animatable;
                mAnimatable=drawable2;
            }

            @Override
            public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

            }

            @Override
            public void onIntermediateImageFailed(String id, Throwable throwable) {

            }

            @Override
            public void onFailure(String id, Throwable throwable) {

            }

            @Override
            public void onRelease(String id) {

            }
        });
        this.setController(builder.build());

    }






    public void start(){
        if(mAnimatable!=null){
            mAnimatable.start();
        }
    }

    public boolean isRuning(){
        if(mAnimatable!=null){
           return mAnimatable.isRunning();
        }
        return false;
    }

    public void stop(){
        if(mAnimatable!=null){
            mAnimatable.stop();
        }
    }

    public void setPlayControRepeat(boolean autoplay, AnimationListener animationListener) {
        builder.setAutoPlayAnimations(autoplay);
        builder.setControllerListener(new ControllerListener<ImageInfo>() {
            @Override
            public void onSubmit(String id, Object callerContext) {
                Log.d("setAutoPlayControRepeat","onSubmit");
            }

            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                Log.d("setAutoPlayControRepeat","onFinalImageSet");
                AnimatedDrawable2 drawable2 = (AnimatedDrawable2) animatable;
                mAnimatable=drawable2;
                if(animationListener!=null){
                    drawable2.setAnimationListener(animationListener);
                }

            }

            @Override
            public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
                Log.d("setAutoPlayControRepeat","onIntermediateImageSet");
            }

            @Override
            public void onIntermediateImageFailed(String id, Throwable throwable) {
                Log.d("setAutoPlayControRepeat","onIntermediateImageFailed");
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                Log.d("setAutoPlayControRepeat","onFailure");
            }

            @Override
            public void onRelease(String id) {
                Log.d("setAutoPlayControRepeat","onRelease");
            }
        });
        this.setController(builder.build());
    }

}
